package br.com.ordersys.web.client.modules.uicomp;


import br.com.ordersys.web.client.modules.utils.I18nBundle;
import br.com.ordersys.web.client.modules.utils.UIUtils;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

public class OptionPopup extends BasicPopup {
	private OptionPopupCallBack callBack;
	private HTML message;
	private static I18nBundle i18n = new I18nBundle("uiComp");
	
	static{
		BasicResources.INSTANCE.basicStyle().ensureInjected();
	}
	
	public OptionPopup(String[] options) {
		super(false,false);
		setTitle(i18n.get("title_confirmation"));
		buildUI(options);
	}
	
	public void addCallBack(OptionPopupCallBack callBack) {
		this.callBack = callBack;
	}
	
	public void setMessage(String msg){
		message.setHTML("<b><p style='text-align: center'>" + msg + "</p></b>");
	}
 
	private void buildUI(String[] options) {
		HorizontalPanel buttonsPane = new HorizontalPanel();
		buttonsPane.getElement().setAttribute("align", "center");
		
		ClickHandler clickHnd = new ClickHandler() {
			@Override
			public void onClick(ClickEvent e) {
				int index = Integer.parseInt(((Widget) e.getSource()).getElement().getAttribute("optionIndex"));
				OptionPopup.this.hide();
				callBack.onSelect(index);
			}
		};

		int i = 0;
		for (String opt : options) {
			Button optBtn = new Button(opt);
			optBtn.setStyleName(BasicResources.INSTANCE.basicStyle().defaultButton());
			optBtn.getElement().setAttribute("optionIndex", String.valueOf(i)); //inserimos um atributo para identificar esse botão no evento
			optBtn.addClickHandler(clickHnd);

			if(i > 0){
				//adicionamos um espaço em branco entre os botões
				buttonsPane.add(UIUtils.buildEmptySpace("1em", "0px"));
			}
			
			buttonsPane.add(optBtn);
			i++;
		}
		
		VerticalPanel vpnl = new VerticalPanel();
		vpnl.setSize("100%","100%");
		
		vpnl.add((message = new HTML()));
		vpnl.add(buttonsPane);

		setBodyPanel(vpnl);
	}
	
	public interface OptionPopupCallBack { 
		void onSelect(int item);
	}
	
	/**
	 * Método de conveniência para mostrar um OptionPopup para confirmação
	 * @param text
	 * @param width
	 * @param height
	 * @param confirmedCallBack
	 */
	public static void showConfirmationDialog(String text, String width, String height,final Runnable confirmedCallBack){
		OptionPopup optionPopup = new OptionPopup(new String[] { i18n.get("btn_yes"), i18n.get("btn_no") });
		optionPopup.setMessage(text);

		optionPopup.addCallBack(new OptionPopupCallBack() {
			@Override
			public void onSelect(int item) {
				if (item == 0) {
					confirmedCallBack.run();
				}
			}
		});
		optionPopup.setSize(width, height);
		optionPopup.center();
	}	
}
